﻿
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using CommonTagTime;
using CommonTagTime.ModelObject;
using CommonTagTime.Business;
namespace CommonTagTime.DataAccess
{
    public class daoCustomer
    {
        public daoCustomer()
        {

        }
        private Customer Load(IDataReader de)
        {
            Customer item = new Customer();
            //item.CategoryID = clsConvert.Int(clsString.CheckObject(de["CategoryID"]));
            item.CustomerId = clsConvert.DbToInt("CustomerId", de);
            item.Name = clsConvert.DbToString("Name", de);
            item.IsActive = clsConvert.DbToBoolean("IsActive", de);
            return item;
            // de.P
            //int intLogID = Convert.ToInt32(_comm.Parameters["@LogID"].Value);
        }


        public CustomerCollection ListAll()
        {
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[6];
            SqlDataReader de;
            CustomerCollection items = new CustomerCollection();

            try
            {

                de = clsSQL.SPDataReader("Customer_GetAll");
                while (de.Read())
                {
                    Customer item = Load(de);
                    items.Add(item);
                }

                de.Close();

                de.Dispose();
            }
            catch (Exception e)
            {
                clsFile.WriteLog("daoCustomer", "ListAll()", e.ToString());
            }
            finally
            {
                de = null;
                clsSQL.Dispose();
                clsSQL = null;
            }

            return items;
        }
        public Customer GetById(int customerId)
        {
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[1];
            SqlDataReader de;
            Customer items =null;

            try
            {
                para[0] = new SqlParameter("CustomerId", SqlDbType.NVarChar);
                para[0].Value = customerId;
                de = clsSQL.SPDataReader("Customer_GetById", para);
                if (de.Read())
                {
                    items = Load(de);
                }
                de.Close();

                de.Dispose();
            }
            catch (Exception e)
            {
                clsFile.WriteLog("daoCustomer", " GetById(int customerId)", e.ToString());
            }
            finally
            {
                de = null;
                clsSQL.Dispose();
                clsSQL = null;
            }

            return items;
        }
        public bool Active(int CustomerID)
        {
            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[1];
            try
            {
                para[0] = new SqlParameter(busCustomer.CUSTOMERID, SqlDbType.Int);
                para[0].Value = CustomerID;
                result = clsSQL.SPExecute("Customer_Active", para);

            }
            catch (Exception ex)
            {
                clsFile.WriteLog("daoCustomer", "Active(int CustomerID)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            if (result > 0)
                return true;
            return false;
        }
        public bool DeActive(int CustomerID)
        {
            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[1];
            try
            {
                para[0] = new SqlParameter(busCustomer.CUSTOMERID, SqlDbType.Int);
                para[0].Value = CustomerID;
                result = clsSQL.SPExecute("Customer_DeActive", para);
            }
            catch (Exception ex)
            {
                clsFile.WriteLog("daoCustomer", "DeActive(int CustomerID)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            if (result > 0)
                return true;
            return false;
        }
        public bool Delete(int CustomerID)
        {
            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[1];
            try
            {
                para[0] = new SqlParameter(busCustomer.CUSTOMERID, SqlDbType.Int);
                para[0].Value = CustomerID;
                result = clsSQL.SPExecute("Customer_Delete", para);
            }
            catch (Exception ex)
            {
                clsFile.WriteLog("daoCustomer", "Delete(int CustomerID)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            if (result > 0)
                return true;
            return false;
        }
        public int Insert(Customer objCustomer)
        {

            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[2];
            try
            {
                para[0] = new SqlParameter(busCustomer.ISACTIVE, SqlDbType.Bit);
                para[0].Value = objCustomer.IsActive;

                para[1] = new SqlParameter(busCustomer.NAME, SqlDbType.NVarChar);
                para[1].Value = objCustomer.Name;

                result = clsSQL.SPExecute("Customer_Insert", para);

            }
            catch (Exception ex)
            {
                clsFile.WriteLog("daoCustomer", "Customer_Insert(Customer Customer)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            return result;
        }
        public int Update(Customer objCustomer)
        {
            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[3];
            try
            {
                para[1] = new SqlParameter(busCustomer.ISACTIVE, SqlDbType.Bit);
                para[1].Value = objCustomer.IsActive;
                para[2] = new SqlParameter(busCustomer.NAME, SqlDbType.NVarChar);
                para[2].Value = objCustomer.Name;
                para[0] = new SqlParameter(busCustomer.CUSTOMERID, SqlDbType.Int);
                para[0].Value = objCustomer.CustomerId;
                result = clsSQL.SPExecute("Customer_Update", para);
            }
            catch (Exception ex)
            {
                clsFile.WriteLog("daoCustomer", "Update(Customer objCustomer)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            return result;
        }
        public CustomerCollection List(Customer customer)
        {
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[6];
            SqlDataReader de;
            CustomerCollection items = new CustomerCollection();

            try
            {
                para[0] = new SqlParameter(busCategory.ORDERBY, SqlDbType.NVarChar);
                para[0].Value = customer.OrderBy;
                para[1] = new SqlParameter(busCategory.ORDERDIRECTION, SqlDbType.NVarChar);
                para[1].Value = customer.OrderDirection;
                para[2] = new SqlParameter(busCategory.CURRENTPAGE, SqlDbType.Int);
                para[2].Value = customer.CurrentPage;
                para[3] = new SqlParameter(busCategory.PAGESIZE, SqlDbType.Int);
                para[3].Value = customer.PageSize;
                para[4] = new SqlParameter(busCategory.CONDITION, SqlDbType.NVarChar);
                para[4].Value = customer.Condition;
                para[5] = new SqlParameter(busCategory.TOTALRECORDS, SqlDbType.Int);
                para[5].Direction = ParameterDirection.Output;
                de = clsSQL.SPDataReader("Customer_List", para);
                while (de.Read())
                {
                    Customer item = Load(de);
                    items.Add(item);
                }
                de.Close();
                customer.TotalRecords = Convert.ToInt32(para[5].Value);
                de.Dispose();
            }
            catch (Exception e)
            {
                clsFile.WriteLog("daoCategory", "List(Customer Customer)", e.ToString());
            }
            finally
            {
                de = null;
                clsSQL.Dispose();
                clsSQL = null;
            }
            return items;
        }
    }
}
